//
//  LTHomeworkDetailVC.m
//  LearnTeacher
//
//  Created by zyl_macbookpro on 2024/4/22.
//

#import "LTHomeworkDetailVC.h"
#import "LTChooseCLassTermView.h"
#import "LTHomeworkReportListView.h"
#import "LTClassAPIManager.h"
#import "LTClassModel.h"

#import "LTStudentLineView.h"
#import "LTEllipseChartView.h"
#import "LTHomeworkGradeListView.h"
#import "LTHomeworkExamListView.h"

#import "LTHomeworkRebackAlertView.h"
@interface LTHomeworkDetailVC ()

@property(nonatomic ,strong)UIScrollView *containView;

@property(nonatomic ,strong)UIView *lineView;

@property(nonatomic ,strong)UIButton *selectedBtn;

@property(nonatomic ,strong)UIView *sliderView;

@property(nonatomic ,strong)UIView *reportView;

@property(nonatomic ,strong)UIView *gradeView;

@property(nonatomic ,strong)UIView *examView;

@property (strong, nonatomic) LTClassModel *classModel;

@property (strong, nonatomic) NSDictionary *data;

@property(nonatomic ,strong)LTHomeworkRebackAlertView *alertView;

@end

@implementation LTHomeworkDetailVC
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:LTClearColor] backgroundColor:LTClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(0, 0, 0, 1),NSFontAttributeName:MFontWithSize(17)} enableScrollEdgeAppearance:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"作业详情";
    // Do any additional setup after loading the view.
    [self setBackButtonIsHidden:NO];
    [self.view addSubview:self.containView];
    
    [self loadData];
}
-(void)loadData
{
    //    NSMutableDictionary *dic = @{@"start":@"2023",@"end":@"2024",@"class_id":self.classModel.class_id}.mutableCopy;
    //    [dic setObject:[NSString stringWithFormat:@"%ld",(long)(self.selectedBtn.tag - 10000 + 1)] forKey:@"type"];
    //    [LTClassAPIManager getHomeworkWithParam:dic result:^(NSDictionary * _Nonnull dic) {
    //        self.data = dic[@"data"];
    //        [self.containView removeFromSuperview];
    //        self.containView = nil;
    //        [self.view addSubview:self.containView];
    //    }];
}
- (void)resetBtnAction
{
    [self.alertView setHidden:NO];
}
- (void)chooseClick:(UIButton *)btn
{
    if (self.selectedBtn != btn) {
        self.selectedBtn = btn;
        double x = self.selectedBtn.centerX;
        if (self.selectedBtn.tag == 10001) {
            x = x - 10;
        }
       
        [UIView animateWithDuration:0.3 animations:^{
            self.lineView.centerX = x;
        }];
        [self loadData];
    }
    
}
- (UIView *)sliderView
{
    if (!_sliderView) {
        _sliderView = [[UIView alloc] initWithFrame:CGRectMake(0,14, LTScreenWidth, 42)];
        _sliderView.backgroundColor = LTClearColor;
        NSArray *title = @[@"作业报告",@"学生成绩",@"题目统计"];
        self.lineView.width = [MethodTool getTextWidthWithStr:title.firstObject font:MFontWithSize(16) ViewHeight:22];
        double x = (LTScreenWidth - ((self.lineView.width + 24)*3 + 20))/2.0;
        for (int i = 0; i < title.count; i++) {
            
            double btnW = self.lineView.width + 34;
            if (i == 1) btnW =  btnW + 20;
            UIButton *sliderBtn = ({
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(x ,0, btnW, 42);
                [button setTitle:title[i] forState:UIControlStateNormal];
                [button setTitleColor:LTTextBlack forState:UIControlStateNormal];
                [button addTarget:self action:@selector(chooseClick:) forControlEvents:UIControlEventTouchUpInside];
                button;
            });
            sliderBtn.width = btnW;
            sliderBtn.tag = i + 10000;
            if (i == 0) {
                self.selectedBtn = sliderBtn;
            }
            if (i == 1) {
                [sliderBtn setImage:IMAGENAMED(@"ic_homework_shuaxin") forState:UIControlStateNormal];
                [sliderBtn layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleRight imageTitleSpace:4];
            }
            x = sliderBtn.right;
            
            [_sliderView addSubview:sliderBtn];
        }
        
        [_sliderView addSubview: self.lineView];
        self.lineView.centerX = self.selectedBtn.centerX;
    }
    return _sliderView;
}
- (UIScrollView *)containView
{
    if (!_containView) {
        _containView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LTTopBarHeight, LTScreenWidth, LTScreenHeight- LTTopBarHeight - 45)];
        _containView.backgroundColor = LTViewBgGray;
        _containView.showsVerticalScrollIndicator = NO;
        _containView.showsHorizontalScrollIndicator = NO;
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(16, 16, LTScreenWidth - 32,164)];
        headerView.backgroundColor = LTWhiteColor;
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0, 0, LTScreenWidth - 32,164);
        gl.startPoint = CGPointMake(0.5, 0.02);
        gl.endPoint = CGPointMake(0.5, 0.2);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:242/255.0 green:247/255.0 blue:253/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        headerView.layer.cornerRadius = 12;
        headerView.layer.masksToBounds = YES;
        headerView.layer.shadowColor = [UIColor colorWithRed:41/255.0 green:121/255.0 blue:255/255.0 alpha:0.0500].CGColor;
        headerView.layer.shadowOffset = CGSizeMake(0,2);
        headerView.layer.shadowOpacity = 1;
        headerView.layer.borderColor = rgba(41, 121, 255, 1).CGColor;
        headerView.layer.borderWidth = 1;
        [headerView.layer addSublayer:gl];
        [_containView addSubview:headerView];
        
        UIButton *resetBtn =  ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.titleLabel.font = RFontWithSize(12);
            [button setFrame:CGRectMake(headerView.width - 16 - 60, 16, 60, 24)];
            [button setTitle:@"重 做" forState:UIControlStateNormal];
            [button setTitleColor:LTWhiteColor forState:UIControlStateNormal];
            [button addTarget:self action:@selector(resetBtnAction) forControlEvents:UIControlEventTouchUpInside];
            button.adjustsImageWhenHighlighted = NO;
            button.layer.backgroundColor = [UIColor colorWithRed:41/255.0 green:121/255.0 blue:255/255.0 alpha:1.0].CGColor;
            button.layer.cornerRadius = 12;
            button.layer.shadowColor = [UIColor colorWithRed:41/255.0 green:121/255.0 blue:255/255.0 alpha:0.5000].CGColor;
            button.layer.shadowOffset = CGSizeMake(0,4);
            button.layer.shadowOpacity = 1;
            button.layer.shadowRadius = 6;
            button;
            
        });
        [headerView addSubview:resetBtn];
        
        UIButton *withdrawBtn =  ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.titleLabel.font = RFontWithSize(12);
            [button setFrame:CGRectMake(headerView.width - 16 - 60 - 10 - 60, 16, 60, 24)];
            [button setTitle:@"撤 回" forState:UIControlStateNormal];
            [button setTitleColor:LTWhiteColor forState:UIControlStateNormal];
            [button addTarget:self action:@selector(resetBtnAction) forControlEvents:UIControlEventTouchUpInside];
            button.adjustsImageWhenHighlighted = NO;
            button.layer.backgroundColor = [UIColor colorWithRed:41/255.0 green:121/255.0 blue:255/255.0 alpha:1.0].CGColor;
            button.layer.cornerRadius = 12;
            button.layer.shadowColor = [UIColor colorWithRed:41/255.0 green:121/255.0 blue:255/255.0 alpha:0.5000].CGColor;
            button.layer.shadowOffset = CGSizeMake(0,4);
            button.layer.shadowOpacity = 1;
            button.layer.shadowRadius = 6;
            button;
            
        });
        [headerView addSubview:withdrawBtn];
        
        
        
        UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 16,headerView.width - 16 - 60 - 10 - 60 - 10 - 16, 18)];
        timeLabel.font = RFontWithSize(13);
        timeLabel.text = [NSString stringWithFormat:@"截止时间：%@",@"9月21日 23:59"];
        [timeLabel setTextAlignment:NSTextAlignmentLeft];
        timeLabel.textColor = rgba(114, 122, 142, 1);
        [headerView addSubview:timeLabel];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, timeLabel.bottom + 6 ,withdrawBtn.left - 16, 18)];
        titleLabel.font = MFontWithSize(16);
        titleLabel.text = [NSString stringWithFormat:@"Module 1 B卷 %@",@"9月21日 23:59"];
        [titleLabel setTextAlignment:NSTextAlignmentLeft];
        titleLabel.textColor = LTTextBlack;
        [headerView addSubview:titleLabel];
        
        UILabel *classLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 122,headerView.width - 16 - 60 - 10 - 60 - 10 - 16, 18)];
        classLabel.font = RFontWithSize(13);
        [classLabel setTextAlignment:NSTextAlignmentLeft];
        NSString *str = [NSString stringWithFormat:@"八年级2班｜%@",@"已完成43/49人"];
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str];
        [att addAttribute:NSForegroundColorAttributeName value:rgba(114, 122, 142, 0.20) range:[str rangeOfString:@"｜"]];
        classLabel.attributedText = att;
        classLabel.textColor = rgba(114, 122, 142, 1);
        [headerView addSubview:classLabel];
        
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(headerView.width - 16 - 96, withdrawBtn.bottom + 10, 96, 96)];
        imgView.image = IMAGENAMED(@"ic_homework_header_circle");
        [headerView addSubview:imgView];
        
        UILabel *sorceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20,76, 28)];
        sorceLabel.font = SFontWithSize(20);
        sorceLabel.text = [NSString stringWithFormat:@"%@",@"67.5"];
        [sorceLabel setTextAlignment:NSTextAlignmentCenter];
        sorceLabel.textColor = rgba(41, 121, 255, 1);
        [imgView addSubview:sorceLabel];
        
        UILabel *sorceTLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, sorceLabel.bottom,76, 17)];
        sorceTLabel.font = SFontWithSize(12);
        sorceTLabel.text = [NSString stringWithFormat:@"%@",@"班级平均分"];
        [sorceTLabel setTextAlignment:NSTextAlignmentCenter];
        sorceTLabel.textColor = rgba(114, 122, 142, 1);
        [imgView addSubview:sorceTLabel];
        
        
        UILabel *sorceAllLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, sorceTLabel.bottom + 2,76, 11)];
        sorceAllLabel.font = RFontWithSize(8);
        sorceAllLabel.text = [NSString stringWithFormat:@"%@",@"作业总分30分"];
        [sorceAllLabel setTextAlignment:NSTextAlignmentCenter];
        sorceAllLabel.textColor = rgba(153, 153, 153, 1);
        [imgView addSubview:sorceAllLabel];
        
        [_containView addSubview:self.sliderView];
        self.sliderView.y = headerView.bottom + 14;
        [_containView addSubview:separateLineWithColor(CGRectMake(0, self.sliderView.bottom, LTScreenWidth, 1), rgba(238, 238, 238, 1))];
        
        
    }
    return _containView;
}
- (UIView *)reportView
{
    if (!_reportView) {
        _reportView = [[UIView alloc] initWithFrame:CGRectMake(0,241, LTScreenWidth, 42)];
        _reportView.backgroundColor = LTClearColor;
        LTHomeworkReportListView *listView = [[LTHomeworkReportListView alloc] initWithFrame:CGRectMake(16, 0, LTScreenWidth - 32, 34 + 2 * 28 + 16 + 16)];
//        NSMutableArray *data = @[].mutableCopy;
//        NSArray *array = self.data[@"list"];
//        NSArray *key = @[@"date",@"people",@"countScore",@"goal",@"average"];
        
        listView.titles = @[@"平均分",@"最高分",@"最低分",@"优秀率\n(≥90)",@"及格率\n(≥60)"];
        listView.data =  @[@[@"45",@"45",@"45",@"45",@"45"],@[@"45",@"45",@"45",@"45",@"45"]];
        [_reportView addSubview:listView];
    
        [listView setAllCornersWithCornerRadius:12];
        _reportView.height = listView.bottom;
        
        UIView *ellipseChartViewContent = [[UIView alloc] initWithFrame:CGRectMake(16, listView.bottom + 20, LTScreenWidth - 32, 310)];
        ellipseChartViewContent.backgroundColor = LTWhiteColor;
        LTEllipseChartView *viewE = [[LTEllipseChartView alloc] initWithFrame:CGRectMake(0, 55, LTScreenWidth - 32, 239)];
        viewE.unitStr = @"人数 (人)";
        viewE.arrayX = @[@"0-20%",@"20-40%",@"40-60%",@"60-80%",@"80-100%"].mutableCopy;
        viewE.arrayY = @[@"34",@"45",@"12",@"32",@"23"].mutableCopy;
        [ellipseChartViewContent addSubview:viewE];
        [ellipseChartViewContent setAllCornersWithCornerRadius:12];
        
        
        UILabel *tLabel = [[UILabel alloc] initWithFrame:CGRectMake(39, 17,ellipseChartViewContent.width - 178, 17)];
        tLabel.font = SFontWithSize(16);
        [tLabel setTextAlignment:NSTextAlignmentLeft];
        NSString *str = [NSString stringWithFormat:@"分数段分析 (得分率分布)"];
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str];
        [att addAttribute:NSForegroundColorAttributeName value:rgba(114, 122, 142, 1) range:[str rangeOfString:@"(得分率分布)"]];
        [att addAttribute:NSFontAttributeName value:RFontWithSize(13) range:[str rangeOfString:@"(得分率分布)"]];
        tLabel.attributedText = att;
        tLabel.textColor = rgba(34, 34, 34, 1);
        [ellipseChartViewContent addSubview:tLabel];
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(16, tLabel.top + 3, 12, 12)];
        imgView.image = IMAGENAMED(@"ic_homework_title_icon");
        [ellipseChartViewContent addSubview:imgView];
        [ellipseChartViewContent addSubview:separateLineWithColor(CGRectMake(16, 54, LTScreenWidth - 64, 1), rgba(238, 238, 238, 1))];

        [_reportView addSubview:ellipseChartViewContent];
        _reportView.height = ellipseChartViewContent.bottom;
        
        //题型分析
        UIView *lineChartViewContent = [[UIView alloc] initWithFrame:CGRectMake(16, ellipseChartViewContent.bottom + 20, LTScreenWidth - 32, 310)];
        lineChartViewContent.backgroundColor = LTWhiteColor;
        [lineChartViewContent setAllCornersWithCornerRadius:12];
        UILabel *tLLabel = [[UILabel alloc] initWithFrame:CGRectMake(39, 17,lineChartViewContent.width - 178, 17)];
        tLLabel.font = SFontWithSize(16);
        [tLLabel setTextAlignment:NSTextAlignmentLeft];
        NSString *strL = [NSString stringWithFormat:@"题型分析"];
        tLLabel.text = strL;
        tLLabel.textColor = rgba(34, 34, 34, 1);
        [lineChartViewContent addSubview:tLLabel];
        
        LTHomeworkReportListView *lineListView = [[LTHomeworkReportListView alloc] initWithFrame:CGRectMake(0, 54, LTScreenWidth - 32, 34 + 2 * 28 + 16 + 16)];
        lineListView.titles = @[@"平均分",@"最高分",@"最低分",@"优秀率\n(≥90)",@"及格率\n(≥60)"];
        lineListView.data =  @[@[@"45",@"45",@"45",@"45",@"45"],@[@"45",@"45",@"45",@"45",@"45"]];
        [lineChartViewContent addSubview:lineListView];
        
        UIImageView *imgViewL = [[UIImageView alloc] initWithFrame:CGRectMake(16, tLabel.top + 3, 12, 12)];
        imgViewL.image = IMAGENAMED(@"ic_homework_title_icon");
        [lineChartViewContent addSubview:imgViewL];
        [lineChartViewContent addSubview:separateLineWithColor(CGRectMake(16, 54, LTScreenWidth - 64, 1), rgba(238, 238, 238, 1))];

        NSMutableArray *arrayX = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *arrayY = [NSMutableArray arrayWithCapacity:0];
        LTStudentLineView *maskLineView = [[LTStudentLineView alloc]initWithFrame:CGRectMake(16, lineListView.bottom + 4, LTScreenWidth - 32 - 35, 247)];
       
        [lineChartViewContent addSubview:maskLineView];
//        if (self.data[@"linear"][@"date"]) {
//            [arrayX addObjectsFromArray:self.data[@"linear"][@"date"]];
//        }
//        if (self.data[@"linear"][@"goal"]) {
//            [arrayY addObjectsFromArray:self.data[@"linear"][@"goal"]];
//        }
        arrayX = @[@"题型",@"题型",@"题型",@"题型",@"题型"].mutableCopy;
        arrayY = @[@"43",@"454",@"42",@"12",@"23"].mutableCopy;
        if (arrayX.count > 0&&arrayY.count > 0&&arrayX.count == arrayY.count) {
            maskLineView.unitStr = @"得分率(%)";
            maskLineView.arrayX = arrayX;
            maskLineView.arrayY = arrayY;
        }
        
        [_reportView addSubview:lineChartViewContent];
        lineChartViewContent.height = maskLineView.bottom;
        
        _reportView.height = lineChartViewContent.bottom;
        
        self.containView.contentSize = CGSizeMake(LTScreenWidth, _reportView.bottom);
        
        [self.containView addSubview:_reportView];
        
    }
    return _reportView;
}
- (UIView *)gradeView
{
    if (!_gradeView) {
        _gradeView = [[UIView alloc] initWithFrame:CGRectMake(0,241, LTScreenWidth, 42)];
        _gradeView.backgroundColor = LTClearColor;
        LTHomeworkGradeListView *listView = [[LTHomeworkGradeListView alloc] initWithFrame:CGRectMake(16, 0, LTScreenWidth - 32, 46 + 2 * 36 + 16 + 16)];
        listView.titles = @[@"姓名",@"开始时间",@"进度",@"得分(30分)"];
        listView.data =  @[@[@"祁晓轩",@"9/219:20",@"100%",@"29.37分"],@[@"祁晓轩",@"9/219:20",@"100%",@"29.37分"]];
        [_gradeView addSubview:listView];
    
        [listView setAllCornersWithCornerRadius:12];
        _gradeView.height = listView.bottom;
        [self.containView addSubview:_gradeView];
        
    }
    return _gradeView;
}
- (UIView *)examView
{
    if (!_examView) {
        _examView = [[UIView alloc] initWithFrame:CGRectMake(0,241, LTScreenWidth, 42)];
        _examView.backgroundColor = LTClearColor;
        LTHomeworkExamListView *listView = [[LTHomeworkExamListView alloc] initWithFrame:CGRectMake(16, 0, LTScreenWidth - 32, 46 + 2 * 36 + 16 + 16)];
        listView.titles = @[@"共1个作业内容",@"平均分/总分"];
        listView.data =  @[@[@"Module 1 B卷",@"15.75/30"],@[@"Module 1 B卷",@"15.75/30"]];
        [_examView addSubview:listView];
    
        [listView setAllCornersWithCornerRadius:12];
        _examView.height = listView.bottom;
        [self.containView addSubview:_examView];
        
    }
    return _examView;
}
- (UIView *)lineView
{
    if(!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.frame = CGRectMake(95,40,28,3);
        _lineView.backgroundColor = rgba(41, 121, 255, 1);
        [_lineView setAllCornersWithCornerRadius:1.5];
    }
    return _lineView;
}
- (LTHomeworkRebackAlertView *)alertView
{
    if (!_alertView) {
        _alertView = [[NSBundle mainBundle] loadNibNamed:@"LTHomeworkRebackAlertView" owner:nil options:nil].lastObject;
        _alertView.hidden = YES;
        WeakSelf(self);
        _alertView.block = ^{
           
        };
        [self.navigationController.tabBarController.view addSubview:_alertView];
    }
    return _alertView;
}
- (void)setSelectedBtn:(UIButton *)selectedBtn
{
    _selectedBtn.titleLabel.font = RFontWithSize(18.f);
    _selectedBtn = selectedBtn;
    _selectedBtn.titleLabel.font = MFontWithSize(18.f);
    self.gradeView.hidden = self.reportView.hidden = self.examView.hidden = YES;
    if (self.selectedBtn.tag == 10001) {
        self.gradeView.hidden = NO;
        self.containView.contentSize = CGSizeMake(LTScreenWidth, _gradeView.bottom);
    }
    if (self.selectedBtn.tag == 10000) {
        self.reportView.hidden = NO;
        self.containView.contentSize = CGSizeMake(LTScreenWidth, _reportView.bottom);
    }
    
    if (self.selectedBtn.tag == 10002) {
        self.examView.hidden = NO;
        self.containView.contentSize = CGSizeMake(LTScreenWidth, _examView.bottom);
    }
    
}
- (void)setParaObject:(NSObject *)paraObject
{
    self.classModel = (LTClassModel *)paraObject;
}
@end
